package com.zmx.common.common.config.Interceptor;


import com.google.common.base.Strings;
import com.zmx.common.common.invocation.InvocationInfo;
import com.zmx.common.common.invocation.InvocationInfoProxy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: cookie拦截器
 * @ClassName: CookieInterceptor
 * @Author zhaomxr
 * @Date 2021-09-13 14:35
 */
@Slf4j
@Component
public class CookieInterceptor implements HandlerInterceptor {

    //tomact会将自定义header默认转为不区分大小写，都是小写
    private static final String COOKIE = "cookies";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String cookies = request.getHeader(COOKIE);

        if (Strings.isNullOrEmpty(cookies)) {
            return true;
        }

        //cookie信息放入本地线程变量中
        InvocationInfoProxy.set(new InvocationInfo(cookies));
        return true;
    }
}
